﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using HslCommunication.Core.Net;

namespace HslCommunication.DTU
{
	/// <summary>
	/// DTU的服务器信息，本服务器支持任意的hsl支持的网络对象，包括plc信息，modbus设备等等，通过DTU来连接，
	/// 然后支持多个连接对象。如果需要支持非hsl的注册报文，需要重写相关的方法<br />
	/// DTU server information, the server supports any network objects supported by hsl, 
	/// including plc information, modbus devices, etc., connected through DTU, and then supports multiple connection objects. 
	/// If you need to support non-HSL registration messages, you need to rewrite the relevant methods
	/// </summary>
	/// <remarks>
	/// 针对异形客户端进行扩展信息
	/// </remarks>
	// Token: 0x02000108 RID: 264
	public class DTUServer : NetworkAlienClient
	{
		/// <summary>
		/// 根据配置的列表信息来实例化相关的DTU服务器<br />
		/// Instantiate the relevant DTU server according to the configured list information
		/// </summary>
		/// <param name="dTUSettings">DTU的配置信息</param>
		// Token: 0x06001780 RID: 6016 RVA: 0x00077BA4 File Offset: 0x00075DA4
		public DTUServer(List<DTUSettingType> dTUSettings)
		{
			this.devices = new Dictionary<string, NetworkDeviceBase>();
			base.SetTrustClients(Enumerable.ToArray<string>(Enumerable.Select<DTUSettingType, string>(dTUSettings, (DTUSettingType m) => m.DtuId)));
			for (int i = 0; i < dTUSettings.Count; i++)
			{
				this.devices.Add(dTUSettings[i].DtuId, dTUSettings[i].GetClient());
				this.devices[dTUSettings[i].DtuId].ConnectServer(new AlienSession
				{
					DTU = dTUSettings[i].DtuId,
					IsStatusOk = false
				});
			}
			base.OnClientConnected += this.DTUServer_OnClientConnected;
		}

		/// <summary>
		/// 根据配置的列表信息来实例化相关的DTU服务器<br />
		/// Instantiate the relevant DTU server according to the configured list information
		/// </summary>
		/// <param name="dtuId">Dtu信息</param>
		/// <param name="networkDevices">设备信息</param>
		// Token: 0x06001781 RID: 6017 RVA: 0x00077C80 File Offset: 0x00075E80
		public DTUServer(string[] dtuId, NetworkDeviceBase[] networkDevices)
		{
			this.devices = new Dictionary<string, NetworkDeviceBase>();
			base.SetTrustClients(dtuId);
			for (int i = 0; i < dtuId.Length; i++)
			{
				this.devices.Add(dtuId[i], networkDevices[i]);
				this.devices[dtuId[i]].ConnectServer(new AlienSession
				{
					DTU = dtuId[i],
					IsStatusOk = false
				});
			}
		}

		/// <inheritdoc />
		// Token: 0x06001782 RID: 6018 RVA: 0x00077CFC File Offset: 0x00075EFC
		protected override void CloseAction()
		{
			foreach (KeyValuePair<string, NetworkDeviceBase> keyValuePair in this.devices)
			{
				AlienSession alienSession = keyValuePair.Value.AlienSession;
				bool flag = alienSession != null;
				if (flag)
				{
					alienSession.IsStatusOk = false;
					Socket socket = alienSession.Socket;
					if (socket != null)
					{
						socket.Close();
					}
				}
			}
		}

		/// <inheritdoc />
		// Token: 0x06001783 RID: 6019 RVA: 0x00077D80 File Offset: 0x00075F80
		public override int IsClientOnline(AlienSession session)
		{
			bool flag = this.devices[session.DTU].AlienSession == null;
			int result;
			if (flag)
			{
				result = 0;
			}
			else
			{
				bool isStatusOk = this.devices[session.DTU].AlienSession.IsStatusOk;
				if (isStatusOk)
				{
					result = 1;
				}
				else
				{
					result = 0;
				}
			}
			return result;
		}

		// Token: 0x06001784 RID: 6020 RVA: 0x00077DDA File Offset: 0x00075FDA
		private void DTUServer_OnClientConnected(AlienSession session)
		{
			this.devices[session.DTU].ConnectServer(session);
		}

		/// <summary>
		/// 根据DTU信息获取设备的连接对象<br />
		/// Obtain the connection object of the device according to the DTU information
		/// </summary>
		/// <param name="dtuId">设备的id信息</param>
		/// <returns>设备的对象</returns>
		// Token: 0x17000509 RID: 1289
		public NetworkDeviceBase this[string dtuId]
		{
			get
			{
				return this.devices.ContainsKey(dtuId) ? this.devices[dtuId] : null;
			}
		}

		/// <summary>
		/// 获取所有的会话信息，是否在线，上线的基本信息<br />
		/// Get all the session information, whether it is online, online basic information
		/// </summary>
		/// <returns>会话列表</returns>
		// Token: 0x06001786 RID: 6022 RVA: 0x00077E14 File Offset: 0x00076014
		public AlienSession[] GetAlienSessions()
		{
			return Enumerable.ToArray<AlienSession>(Enumerable.Select<NetworkDeviceBase, AlienSession>(this.devices.Values, (NetworkDeviceBase m) => m.AlienSession));
		}

		/// <summary>
		/// 获取所有的设备的信息，可以用来读写设备的数据信息<br />
		/// Get all device information, can be used to read and write device data information
		/// </summary>
		/// <returns>设备数组</returns>
		// Token: 0x06001787 RID: 6023 RVA: 0x00077E4A File Offset: 0x0007604A
		public NetworkDeviceBase[] GetDevices()
		{
			return Enumerable.ToArray<NetworkDeviceBase>(this.devices.Values);
		}

		/// <inheritdoc />
		// Token: 0x06001788 RID: 6024 RVA: 0x00077E5C File Offset: 0x0007605C
		public override string ToString()
		{
			return string.Format("Dtu[{0}]", base.Port);
		}

		// Token: 0x04000531 RID: 1329
		private Dictionary<string, NetworkDeviceBase> devices;
	}
}
